package com.ehotting.mqbd.core.call;

import com.alibaba.fastjson.JSON;
import com.ehotting.mqbd.api.bean.*;
import com.ehotting.mqbd.api.bean.solver.SolverStateBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public abstract class HttpCallerAbstract extends CallerAbstract {

    private static Logger logger = LoggerFactory.getLogger(HttpCallerAbstract.class);

    @Override
    public void invoke(SolverStateBean solverState, MQDataAble mqData) throws Exception {
        MQHttpData data  = (MQHttpData) mqData;
        replaceUrl(solverState,data.getRequest());
        super.invoke( solverState,  mqData);

    }

    protected CommonResponse packCommonResponse(String sync, Map<String, Object> webInfo) {
        CommonResponse commonResponse = new CommonResponse();
        commonResponse.setWebInfo(webInfo);
        if (sync != null) {
            Map<String,Object> customResponse = JSON.parseObject(sync, Map.class);
            commonResponse.setResponse(customResponse);
        }
        return commonResponse;
    }

    public void replaceUrl(SolverStateBean solverState, CommonRequest request){
        logger.info("request.getRequestUrl():{}",request.getRequestUrl());
        request.setRequestUrl(super.replaceUrl(solverState,request.getRequestUrl()));
    }


}